package com.senven.api.exception;

import com.senven.api.commom.ResponseResult;
import com.senven.api.utils.ReponseResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author LQP
 */
@ControllerAdvice
public class ExceptionHandle {
    private Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exceptinDispose(Exception e) {
        if (e instanceof GlobalException) {
            logger.error(((GlobalException) e).getMsg());
            return ReponseResultUtil.error(((GlobalException) e).getCode(),((GlobalException) e).getMsg());
            //return ServerResponse.buildErrorCodeMsg(((GlobalException) e).getMsg(), ((GlobalException) e).getCode());
        } else {
            logger.error("发生错误:" + e.getMessage());
            return ReponseResultUtil.error(-1,"系统未知异常");
            //return ServerResponse.buildErrorMsg(e.getMessage());
        }
    }
}
